package net.zabuchy.user.action;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import net.zabuchy.address.entity.Address;
import net.zabuchy.helper.AddressHelper;
import net.zabuchy.helper.EmailHelper;
import net.zabuchy.helper.ParamNames;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.loginHelper.UserRoles;
import net.zabuchy.meeting.entity.Meeting;
import net.zabuchy.project.entity.Project;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class CreateUserAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	@Autowired
	private UserBo userBo;

	@Autowired
	private SessionHelper sessionBean;
	
	@Autowired
	private EmailHelper emailHelper;

	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private static final Logger LOGGER = Logger
			.getLogger(CreateUserAction.class.getName());

	private int userId;
	private int customerId;
	private String username;
	private String password;
	private boolean enabled;
	private String firstName;
	private String secondName;
	private String email;
	private String fax;
	private String phone;
	private Address address;
	private Date creationDate;
	
	private Set<String> roles = UserRoles.getAllRoles();
	private String choosenRole;
	
	private User user;
	/**********************************************/
	private String city;
	private String country;
	private String address1;
	private String state;
	private String zipcode;
	private String[] countries = AddressHelper.getCountries();
	/**********************************************/
	
	

	public String execute() {

		LOGGER.debug("start");

		String returnInputUserForm = request
				.getParameter(ParamNames.CREATE_NEW_USER_FORM);

		boolean returnForm = (returnInputUserForm != null ? Boolean
				.parseBoolean(returnInputUserForm) : false);

		if (returnForm) {
			LOGGER.debug("clear all fields and return form");
			clearFields();
			return Action.INPUT;
		}

		if (userBo == null) {
			return Action.ERROR;
		}

		customerId = sessionBean.getCustomerId();
		creationDate = new Date(System.currentTimeMillis());
		enabled = true;
		
		User user = new User(
				customerId,
				username,
				password,
				enabled, 
				firstName,
				secondName, 
				email,
				fax, 
				phone, 
				null, 
				creationDate,
				null);
				
		userBo.add(user);
		userBo.addUserRole(choosenRole, user.getUserId());
		//emailHelper.sendMail("dupa", email, "Welcome in our system!", "Hello!");
		this.userId = user.getUserId();
		
		return Action.SUCCESS;
	}
	
	public String addUserAddress() {
		LOGGER.debug("userId: " + userId);
		Address address = new Address();
		address.setCity(city);
		address.setCountry(country);
		address.setState(state);
		address.setZipcode(zipcode);
		address.setAddress1(address1);
		
		user = userBo.findById(userId);
		user.setAddress(address);
		userBo.update(user);
		
		return Action.SUCCESS;
	}

	private void clearFields() {
		LOGGER.debug("clear fields");
		userId = 0;
		username = null;
		password= null;
		enabled = false;
		firstName = null;
		secondName = null;
		email = null;
		fax = null;
		phone = null;
		address = null;
		creationDate = null;

		city = null;
		country = null;
		address1 = null;
		state = null;
		zipcode = null;
	}

	private User getUser(Integer id) {
		LOGGER.debug("Get user with id = " + id);
		return userBo.findById(id);

	}

	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	/**
	 * @param emailHelper the emailHelper to set
	 */
	public void setEmailHelper(EmailHelper emailHelper) {
		this.emailHelper = emailHelper;
	}

	/**
	 * @return the emailHelper
	 */
	public EmailHelper getEmailHelper() {
		return emailHelper;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getSecondName() {
		return secondName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Set<String> getRoles() {
		return roles;
	}

	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getAddress1() {
		return address1;
	}

	public void setAddress1(String address1) {
		this.address1 = address1;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String[] getCountries() {
		return countries;
	}

	public void setCountries(String[] countries) {
		this.countries = countries;
	}

	public String getChoosenRole() {
		return choosenRole;
	}

	public void setChoosenRole(String choosenRole) {
		this.choosenRole = choosenRole;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getServletRequest() {
		return request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletResponse getServletResponse() {
		return response;
	}
}